
from django.http import JsonResponse
from django.shortcuts import render

from django.views import View
from areas.models import Area, House, Facility
from django import http
import json

class AreasView(View):
    """区域"""

    def get(self, request):

        try:
            # 1.查询区域数据
            aname_list = Area.objects.filter()

            # 2.整理区域数据
            data_list = []
            for aname in aname_list:
                data_list.append({'aid': aname.id,
                                'aname': aname.name})

        except Exception as e:
              # 如果报错, 则返回错误原因:
            return JsonResponse({'errno': 400,
                                 'errmsg': '区域数据错误'})

        # 3.返回整理好的省级数据
        return JsonResponse({'errmsg': '获取成功',
                             'errno': '0',
                             'data': data_list})



class HouseView(View):
    def post(self, request):
        dict=json.loads(request.body)
        title=dict.get('title')
        price=dict.get('price')
        area_id=dict.get('area_id')
        address=dict.get('address')
        room_count=dict.get('room_count')
        unit=dict.get('unit')
        capacity=dict.get('capacity')
        beds=dict.get('beds')
        deposit=dict.get('deposit')
        min_days=dict.get('min_days')
        max_days=dict.get('max_days')
        facility=dict.get('facility')

        if request.user.isnot_authenticated:
            return JsonResponse({'code': 400,
                                 'errmsg': '请登录后重试'})

        #检验参数
        if not all([title,price,area_id,address,room_count,unit,capacity,beds,deposit,min_days,max_days,facility]):
            return JsonResponse({'errno': 400,
                                 'errmsg': '缺少必传参数'})
        #判断金额是否正确
        try:
            price=int(float(price) * 100)
            deposit=int(float(deposit) * 100)
        except Exception as e :
            return JsonResponse({'errno': 400,
                                 'errmsg': '参数错误'})
        #判断城区是否正确
        try:
            area=Area.objects.filter(area_id)
        except Exception as e :
            return JsonResponse({'errno': 400,
                                     'errmsg': '参数错误'})
        try:
            house=House.objects.create(title=title,
                        price=price,
                        area_id=area_id,
                        address=address,
                        room_count=room_count,
                        unit=unit,
                        capacity=capacity,
                        beds=beds,
                        deposit=deposit,
                        min_days=min_days,
                        max_days=max_days,
                        facility=facility,
            )
        except Exception as e:

            return JsonResponse({'errno': 400,
                             'errmsg': '保存数据失败'})

        facility_ids=dict.get('facility')
        if facility_ids:
            try:
                facility =Facility.objects.filter(Facility.id.in_(facility_ids)).all()

            except Exception as e:

                return JsonResponse({'errno': 400,
                                     'errmsg': '设施选择失败'})
            if facility:
                house.facility=facility

            return JsonResponse({'errno': 400,
                                 'errmsg': '设施选择失败'})

















